//
//  RecommendListViewController.m
//  DayByDay
//
//  Created by dllo on 16/5/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RecommendListViewController.h"
#import "RecommendListTableViewCell.h"
#import <AFNetworking.h>
#import "RecommendListModel.h"
#import <UIImageView+WebCache.h>
#import "MusicDetailsViewController.h"
#import "SongerListViewController.h"
//#import <objc/runtime.h>

@interface RecommendListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *array;
@property (nonatomic, assign)CGFloat currentContentOffSite_y;
@property (nonatomic, strong)RecommendListModel *recommendModel;
@property (nonatomic, assign)CGFloat currentOffSit_y;
@property (nonatomic, strong)JHJTabBarController *tabBar;


@end

@implementation RecommendListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
    self.array = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.alpha = 0.01;
    AppDelegate *tempAppdelegate = [[UIApplication sharedApplication] delegate];
    self.tabBar = tempAppdelegate.JHJTabBar;
    [self createView];
    [self createData];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

- (void)createData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *firstStr = @"http://api.songlist.ttpod.com/ranklists/";
    NSString *lastStr = @"/current?app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
    NSString *urlStr = [NSString stringWithFormat:@"%@%@%@",firstStr, self.value, lastStr];
//    NSLog(@"%@",urlStr);
    [manager GET:urlStr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        self.recommendModel = [RecommendListModel new];
        [self.recommendModel setValuesForKeysWithDictionary:responseObject];
        UIImageView *imageView = [self.view viewWithTag:24000];
        [imageView sd_setImageWithURL:[NSURL URLWithString:self.recommendModel.detailImageModel.pic]];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"songs" object:self.recommendModel.songs];
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}
- (void)createView {
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.contentInset = UIEdgeInsetsMake((HEIGHT / 3 + 50), 0, 0, 0);
    self.currentContentOffSite_y = -HEIGHT / 3 - 50;
    self.currentOffSit_y = -HEIGHT / 3 - 50;
    [self.view addSubview:self.tableView];
    
    UIImageView *imageV= [[UIImageView alloc] initWithFrame:CGRectMake(0, -50 - HEIGHT / 3 , WIDTH, HEIGHT / 3)];
    imageV.tag = 24000;
    imageV.backgroundColor = [UIColor redColor];
    UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(0, HEIGHT / 3, WIDTH, 50)];
    view.tag = 25000;
    [self.view addSubview:view];
    view.image = [UIImage imageNamed:@"623a1ad3fc2528c0181f497a5bddc545"];
    
    view.backgroundColor = self.navigationController.navigationBar.barTintColor;
    
    [self.tableView addSubview:imageV];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame];
    imageView.image = [UIImage imageNamed:@"EA839701163D73C926A3C55FB088A084"];
    self.tableView.backgroundView= imageView;
//    self.tableView.bounces = NO;
    UINib *nib = [UINib nibWithNibName:@"RecommendListTableViewCell" bundle:[NSBundle mainBundle]];
    [self.tableView registerNib:nib forCellReuseIdentifier:NSStringFromClass([RecommendListTableViewCell class])];
   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.tableView) {
        //到-114停止;
        if (scrollView.contentOffset.y < -114) {
            self.navigationController.navigationBar.alpha = (scrollView.contentOffset.y + HEIGHT / 3 + 50 + 0.01) * 0.01 / 2;

            self.currentContentOffSite_y = scrollView.contentOffset.y;
            UIImageView *imageV = [self.view viewWithTag:24000];
            if (scrollView.contentOffset.y < -HEIGHT / 3 - 50) {
                imageV.frame = CGRectMake(- ((-scrollView.contentOffset.y - 50) / HEIGHT * 3 * WIDTH - WIDTH) / 2, scrollView.contentOffset.y, ((-scrollView.contentOffset.y - 50) / HEIGHT * 3 *WIDTH), -scrollView.contentOffset.y - 50);
            }
                        [self performSelector:@selector(setImageFrame:) withObject:scrollView afterDelay:0 inModes:@[NSRunLoopCommonModes]];
        }
        if (scrollView.contentOffset.y >= -114) {
            
            [self performSelector:@selector(setViewRect:) withObject:scrollView afterDelay:0 inModes:@[NSRunLoopCommonModes]];
            
        }
//        self.currentContentOffSite_y = scrollView.contentOffset.y;
        //    if (scrollView.contentOffset.y <= -HEIGHT / 3 - 50) {
        //        self.tableView.bounces = YES;
        //    }
        if (scrollView.contentOffset.y > self.currentOffSit_y && scrollView.contentOffset.y > -HEIGHT / 3 - 50) {
            [self.tabBar hiddenTabBarWithAnimation:YES];
            //            self.tableView.bounces = NO;
        } else if (scrollView.contentOffset.y < self.currentOffSit_y && scrollView.contentOffset.y > 0) {
            [self.tabBar showTabBarWithAnimation:YES];
            //            self.tableView.bounces = YES;
        }
        self.currentOffSit_y = scrollView.contentOffset.y;
        
    }
}
- (void)setViewRect:(UIScrollView *)scrollView {
    UIView *view = [self.view viewWithTag:25000];
    [UIView animateWithDuration:0.1 animations:^{
        
        view.frame = CGRectMake(0, 64, WIDTH, 50);
        NSLog(@"%f",view.frame.origin.y);
        
    } completion:^(BOOL finished) {
        
        view.frame = CGRectMake(0, 64, WIDTH, 50);
        NSLog(@"%f",view.frame.origin.y);
        
    }];
    
}
- (void)setImageFrame:(UIScrollView *)scrollView {

    [UIView animateWithDuration:0 animations:^{
        
                            UIView *view = [self.view viewWithTag:25000];
                            view.frame = CGRectMake(0, - scrollView.contentOffset.y - 50, WIDTH, 50);
                        }];
//
}

//-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
//{
//    
//
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"playMusic" object:indexPath];
    //通知中心JHJTabBarControllerCell;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"rotationImage" object:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recommendModel.songs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RecommendListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RecommendListTableViewCell class])];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
    cell.numberLabel.textAlignment = 1;
    cell.model = self.recommendModel.songs[indexPath.row];
    cell.selectionStyle = 0;
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
